package com.derbysoft.dswitch.adapter.ihg.dswitch3.translate.impl;


import com.derbysoft.dswitch.adapter.ihg.dto.OTAHotelAvailRQ;
import com.derbysoft.dswitch.adapter.ihg.dswitch3.translate.Translator;
import com.derbysoft.dswitch.adapter.ihg.webservice.impl.RequestPair;
import com.derbysoft.dswitch.remote.hotel.dto.HotelAvailRequest;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by: jason
 * Date: 2012-5-17
 */
@Component("hotelMultiAvailRQTranslator")
public class HotelMultiAvailRQTranslatorImpl extends BaseTranslator
        implements Translator<HotelAvailRequest, List<RequestPair<OTAHotelAvailRQ>>, Void> {

    @Override
    public List<RequestPair<OTAHotelAvailRQ>> translate(HotelAvailRequest hotelAvailRequest, Void aVoid) {
        List<RequestPair<OTAHotelAvailRQ>> otaHotelAvailRQs = new ArrayList<RequestPair<OTAHotelAvailRQ>>();
        for (String hotelPassport : hotelAvailRequest.getHotelAvailRQ().getAvailCriteria().getHotelCodesList()) {
            otaHotelAvailRQs.add(new RequestPair<OTAHotelAvailRQ>(createHotelAvailRQ(hotelPassport, hotelAvailRequest), hotelPassport));
        }
        setRequestsAttributes(otaHotelAvailRQs, hotelAvailRequest);
        return otaHotelAvailRQs;
    }

    private OTAHotelAvailRQ createHotelAvailRQ(String hotelPassport, HotelAvailRequest hotelAvailRequest) {
        OTAHotelAvailRQ otaHotelAvailRQ = new OTAHotelAvailRQ();
        otaHotelAvailRQ.setAvailRequestSegments(translateAvailRequestSegments(hotelPassport, hotelAvailRequest));
        return otaHotelAvailRQ;
    }

    private void setRequestsAttributes(List<RequestPair<OTAHotelAvailRQ>> otaHotelAvailRQs, HotelAvailRequest hotelAvailRequest) {
        for (RequestPair<OTAHotelAvailRQ> otaHotelAvailRQ : otaHotelAvailRQs) {
            setRequestAttributes(otaHotelAvailRQ.getRequest(), hotelAvailRequest);
        }
    }
}
